package com.gxa.jd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.jd.mapper.GoodsMapper;
import com.gxa.jd.pojo.Goods;
import com.gxa.jd.service.GoodsService;
import com.gxa.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 返回全部记录
     *
     * @return
     */
    @Override
    public List<Goods> findAll() {
        return goodsMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    @Override
    public PageResult<Goods> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Goods> goods = (Page<Goods>) goodsMapper.selectAll();
        return new PageResult<Goods>(goods.getTotal(), goods.getResult());
    }

//    /**
//     * 条件查询
//     *
//     * @param searchMap 查询条件
//     * @return
//     */
//    @Override
//    public List<Goods> findList(Map<String, Object> searchMap) {
//        Example example = createExample(searchMap);
//        return goodsMapper.selectByExample(example);
//    }

//    /**
//     * 分页+条件查询
//     *
//     * @param searchMap
//     * @param page
//     * @param size
//     * @return
//     */
//    @Override
//    public PageResult<Goods> findPage(Map<String, Object> searchMap, int page, int size) {
//        PageHelper.startPage(page, size);
//        Example example = createExample(searchMap);
//        Page<Goods> goods = (Page<Goods>) goodsMapper.selectByExample(example);
//        return new PageResult<Goods>(goods.getTotal(), goods.getResult());
//    }

//    /**
//     * 根据Id查询
//     *
//     * @param id
//     * @return
//     */
//    @Override
//    public Goods findById(Long id) {
//        return goodsMapper.selectByPrimaryKey(id);
//    }

    /**
     * 新增
     *
     * @param goods
     */
    @Override
    public void add(Goods goods) {
        goods.setStock(200L);
        goodsMapper.insert(goods);
    }

    /**
     * 修改
     *
     * @param goods
     */
    @Override
    public void update(Goods goods) {
        goodsMapper.updateByPrimaryKeySelective(goods);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        goodsMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */

    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Goods.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 商品名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 商品货号
            if (searchMap.get("num") != null && !"".equals(searchMap.get("num"))) {
                criteria.andLike("num", "%" + searchMap.get("num") + "%");
            }
            // 商品描述
            if (searchMap.get("description") != null && !"".equals(searchMap.get("description"))) {
                criteria.andLike("description", "%" + searchMap.get("description") + "%");
            }

            // 上架状态 0下架 1上架
            if (searchMap.get("status") != null) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }

        }
        return example;
    }

}
